package sz.szsmk.citizencard.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.chenenyu.router.annotation.Route;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusHomeActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.mvp.contract.IBusHomeContact;
import sz.szsmk.citizencard.mvp.presenter.BusHomePresenter;
import sz.szsmk.citizencard.ui.adapter.MyPagerAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.ui.fragment.BusHomeLineFragment;
import sz.szsmk.citizencard.ui.fragment.BusHomeSiteFragment;
import sz.szsmk.citizencard.ui.fragment.BusHomeTransFragment;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.widget.NoScrollViewPager;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/14 16:02
 * description:公交首页
 */
@Route("BusHomeActivity")
public class BusHomeActivity extends IBaseActivity<BusHomePresenter, BusHomeActivityBinding>
        implements IBusHomeContact.IBusHomeView, View.OnClickListener {

    TextView suggestLine2;
    TextView suggestLine1;
    ImageView back;
    TextView settingTitle;
    TextView bjTv;
    TextView searchEt;
    TextView busHomeHc;
    TextView suggestLine3;
    private BusHomeLineFragment busHomeLineFragment;
    private BusHomeSiteFragment busHomeSiteFragment;
    private List<Fragment> fragmentList = new ArrayList<>();
    private MyPagerAdapter pagerAdapter;
    private TextView busHomeXl1;
    private TextView busHomeZd1;
    private NoScrollViewPager mViewPager;
    private TextView title;
    private TextView busHomeHc1;
    private BusHomeTransFragment busHomeTransFragment;
    private TextView searEt;

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_home_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {


        suggestLine2 = findViewById(R.id.suggest_line2);
        suggestLine1= findViewById(R.id.suggest_line1);
        back= findViewById(R.id.back);
        settingTitle= findViewById(R.id.setting_title);
        bjTv= findViewById(R.id.bj_tv);
        searchEt= findViewById(R.id.search_et);
        busHomeHc= findViewById(R.id.bus_home_hc);
        suggestLine3= findViewById(R.id.suggest_line3);


//        dataBinding.busHomeHc.setTextColor();
        busHomeLineFragment = new BusHomeLineFragment(this);
        busHomeSiteFragment = new BusHomeSiteFragment(this);
        busHomeTransFragment = new BusHomeTransFragment(this);
        busHomeXl1 = (TextView) findViewById(R.id.bus_home_xl);
        busHomeZd1 = (TextView) findViewById(R.id.bus_home_zd);
        busHomeHc1 = (TextView) findViewById(R.id.bus_home_hc);
        searEt = (TextView) findViewById(R.id.search_et);
        title = (TextView) findViewById(R.id.setting_title);
        title.setText("苏州实时公交查询");
        busHomeXl1.setOnClickListener(this);
        busHomeZd1.setOnClickListener(this);
        busHomeHc1.setOnClickListener(this);
        back.setOnClickListener(this);
        searEt.setOnClickListener(this);
        fragmentList.add(busHomeLineFragment);
        fragmentList.add(busHomeSiteFragment);
        fragmentList.add(busHomeTransFragment);
        mViewPager = (NoScrollViewPager) findViewById(R.id.bus_home_vp);
        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
        mViewPager.setAdapter(pagerAdapter);
        mViewPager.setCurrentItem(0);
        searEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    Bundle bundle = new Bundle();
                    JumpUtils.jumptoNatvie(BusHomeActivity.this, "BusSearchHistotyActivity", 0, bundle);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void updateViews() {

    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bus_home_zd) {
            mViewPager.setCurrentItem(1);
            suggestLine3.setVisibility(View.GONE);
            suggestLine2.setVisibility(View.VISIBLE);
            suggestLine1.setVisibility(View.GONE);
            searEt.setText("请输入站点名称");
        } else if (id == R.id.bus_home_xl) {
            mViewPager.setCurrentItem(0);
            searEt.setText("请输入线路名称");
            suggestLine2.setVisibility(View.GONE);
            suggestLine1.setVisibility(View.VISIBLE);
            suggestLine3.setVisibility(View.GONE);
        } else if (id == R.id.bus_home_hc) {
            suggestLine3.setVisibility(View.VISIBLE);
            suggestLine2.setVisibility(View.GONE);
            mViewPager.setCurrentItem(2);
            searEt.setText("请输入线路名称");
            suggestLine1.setVisibility(View.GONE);
        } else if (id == R.id.back) {
            finish();
        } else if (id == R.id.search_et) {
            String string = searEt.getText().toString();

            Bundle bundle = new Bundle();
            if (string.contains("线路")) {
                bundle.putString(Constant.TYPE, "line");
                JumpUtils.jumptoNatvie(this, "BusSearchHistotyActivity", 0, bundle);
            } else if (string.contains("站点")) {

                bundle.putString(Constant.TYPE, "site");
                JumpUtils.jumptoNatvie(this, "BusSearchHistotyActivity", 0, bundle);
            } else if (string.contains("换乘")) {
//                    Toast.makeText(this, "跳转换乘", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    public void queryBusLineNearbyFail() {

    }

    @Override
    public void queryBusLineNearbySucc(QueryBusLineNearbyRes response) {

    }
}
